var express = require('express');
var app = express();
let port = 8001
const Bottleneck = require('bottleneck');
const { firefox } = require('playwright');
const dayjs=require('dayjs')
// 创建限流器
const limiter = new Bottleneck({
    maxConcurrent: 1, // 最大并发数
    minTime:15000
});
app.get('/',limiter.wrap(async (req, res) => {
    // 部署到服务器的完整URL
    var url = req.protocol + '://' + req.hostname + req.originalUrl;
    // let url = req.query.url
    const browser = await firefox.launch({
        headless: true,
    });
    const page = await browser.newPage();
    page.setDefaultTimeout(10000)
    page.goto(url).then(result => {
        setTimeout(async () => {
            let content = await page.content()
            page.close()
            browser.close()
            res.send(content)
            console.log(`成功,${url},${dayjs().format("YYYY-MM-DD HH:mm:ss")}`)
        }, 2000)
    }).catch(async error => {
        // 访问出错,关闭浏览器,重定向至目标页面
        console.log(`失败,${url},${dayjs().format("yyyy-MM-DD HH:mm:ss")}`)
        page.close()
        browser.close()
        res.redirect(url)
    })
}));
const server = app.listen(port, '0.0.0.0', () => {
    console.log('Spider app listening on port ' + port + '!');
});
server.on('error', (err) => {
    console.error('端口绑定错误：', err);
    // 发生错误时退出进程，避免容器假运行
    process.exit(1);
});